//
//  THBabyFinshViewController.m
//  TreeHole
//
//  Created by macbookpro on 2021/10/20.
//  Copyright © 2021 stoneobs. All rights reserved.
//

#import "THBabyFinshViewController.h"
#import "THBabyDetailOrder.h"
#import "STTools_RadioButton.h"
#import "STTools_TextView.h"
@interface THBabyFinshViewController ()
@property(nonatomic, strong) NSArray                     *eva_array;/**<  */
@property(nonatomic, strong) THBabyDetailOrder                     *detailModel;/**<  */
@property(nonatomic, strong) STTools_XingXIngView                     *xingxingView;/**<  */
@property(nonatomic, strong) NSMutableDictionary                     *evaDic;/**<  */

@property(nonatomic, strong) STTools_TextView                     *textView;/**<  */
@end

@implementation THBabyFinshViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"评价";
    [self sendDetailRequest];

    // Do any additional setup after loading the view.
}

#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    
    UIView * header = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    header.backgroundColor = UIColor.whiteColor;
    CGFloat top = 0;
    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 98)
                                                                  title:nil
                                                             titleColor:UIColor.blackColor
                                                              titleFont:15
                                                           cornerRadius:0
                                                        backgroundColor:nil
                                                        backgroundImage:nil
                                                                  image:nil];
        stoneobs_ui_button.titleEdgeInsets = UIEdgeInsetsMake(0, 105, 0, 0);
        [header addSubview:stoneobs_ui_button];
        stoneobs_ui_button.titleLabel.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:20];
        
        
        UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 60, 60)];
        stoneobs_ui_imageView.image = [UIImage imageNamed:@"ic_none"];
        [stoneobs_ui_button addSubview:stoneobs_ui_imageView];
        stoneobs_ui_imageView.layer.cornerRadius = 30;
        stoneobs_ui_imageView.clipsToBounds = YES;
        stoneobs_ui_imageView.centerY = stoneobs_ui_button.height / 2;
        [stoneobs_ui_imageView sd_setImageWithURL:self.detailModel.header_img.mj_url];
        
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 30)
                                                                text:self.detailModel.real_name
                                                           textColor:STONEOBS_UI_FIRST_TEXT_COLOR
                                                                font:16
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        stoneobs_ui_label.centerY = stoneobs_ui_imageView.centerY;
        stoneobs_ui_label.left = stoneobs_ui_imageView.right + 20;
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:18];
        [header addSubview:stoneobs_ui_label];
        
        
        top = stoneobs_ui_button.bottom ;
    });
    
    

    
    ({
        STButton * button = [self viewFormLeftTitle:@"职业年龄" value:self.detailModel.job_age];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    ({
        STButton * button = [self viewFormLeftTitle:@"联系电话" value:self.detailModel.phone];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    ({
        STButton * button = [self viewFormLeftTitle:@"本次服务类目" value:self.detailModel.major];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
    });
    
    
    ({
        
        top = top + 15;
        
        STButton * button = [self viewFormLeftTitle:@"服务评分" value:@""];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
        
        STTools_XingXIngView * xingxing = [STTools_XingXIngView.alloc initWithFrame:CGRectMake(120, 0, 44*5, button.height) maxsNum:5];
        self.xingxingView = xingxing;
        [xingxing stoneobs_fun_st_makeXingXingSelectedWithNum:1];
        [header addSubview:xingxing];
        xingxing.centerY = button.centerY;
        
    });
    __weak typeof(self) weakSelf =  self;
    self.evaDic = NSMutableDictionary.new;
    for (NSInteger i = 0; i < self.detailModel.evaluate_tags.count; i ++) {
        THBabyDetailOrderEvaModel * model = self.detailModel.evaluate_tags[i];
        
        top = top + 10;
        
        STButton * button = [self viewFormLeftTitle:model.key value:@""];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
        
        NSArray * array = @[model.btn1.length?model.btn1:@"",model.btn2.length?model.btn2:@""];
        CGFloat left = 160;
        STTools_RadioButton * radioButton = [[STTools_RadioButton alloc] initWithFrame:CGRectMake(left, 0, UISCREEN_WIDTH - left - 15, 40) titles:array itemSize:CGSizeMake(left/2, 40) itemInset:UIEdgeInsetsMake(0, 0, 0, 0)];
        [header addSubview:radioButton];
        [radioButton setOnSelctedRadioButton:^(STTools_RadioButton *radioButton, NSInteger selectedIndex) {
            [weakSelf.evaDic setObject:array[i] forKey:model.key];
        }];

        radioButton.centerY = button.centerY;
    }

    ({
        
        top = top + 15;
        
        STButton * button = [self viewFormLeftTitle:@"服务体验" value:@""];
        button.top = top;
        top = button.bottom;
        [header addSubview:button];
        

        
    });
    
    self.textView = [[STTools_TextView alloc] initWithFrame:CGRectMake(15, top, UISCREEN_WIDTH-30, 200)];
    [header addSubview:self.textView];
    self.textView.backgroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
    [self.textView stbw_setBorderWith:1 borderColor:UIColor.clearColor cornerRadius:8];
    self.textView.placeholder = @"请输入服务体验";
    top = self.textView.bottom + 20;
    
    [self stoneobs_fun_st_autoAdjustAllResponder];
    header.height = top;
    self.tableView.tableHeaderView = header;
}
- (void)config_table_footer{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 100)];
    
    STButton * button = [[STButton alloc] initWithFrame:CGRectMake(20, 0, UISCREEN_WIDTH - 40, 44)
                                                  title:@"确认完成"
                                             titleColor:UIColor.whiteColor
                                              titleFont:15
                                           cornerRadius:22
                                        backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                        backgroundImage:nil
                                                  image:nil];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    button.centerX = self.tableView.width / 2;
    button.centerY = footer.height /2;
    [footer addSubview:button];
    [button setClicAction:^(UIButton *sender) {
        [weakSelf onselcted_commit_button];
    }];
    self.tableView.tableFooterView = footer;
    
}
- (void)onselcted_commit_button{
    if (self.evaDic.allValues.count != self.detailModel.evaluate_tags.count) {
        [SVProgressHUD showErrorWithStatus:@"请填写完所有信息"];
        return;
    }
    if (self.textView.text.length<1) {
        [SVProgressHUD showErrorWithStatus:@"请填写服务体验"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (self.model.o_id.length ) {
        [stoneobs_net_param_dic setObject:self.model.o_id forKey:@"id"];
    }
    [stoneobs_net_param_dic setObject:@(self.xingxingView.chosedNum) forKey:@"star"];
    NSMutableArray * array = NSMutableArray.new;
    for (NSString * key in self.evaDic.allKeys) {
        [array addObject:@{key:self.evaDic[key]}];
    }
    [stoneobs_net_param_dic setObject:array.mj_JSONString forKey:@"evaluate_tags"];
    [stoneobs_net_param_dic setObject:self.textView.text forKey:@"evaluate"];
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/nursery/finish"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * dataDic = responseObject[@"data"];
        [SVProgressHUD showSuccessWithStatus:@"评价成功"];
        [self.navigationController popViewControllerAnimated:YES];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
    
    
}
- (STButton*)viewFormLeftTitle:(NSString*)leftTitle value:(NSString*)value{
    STButton * view  = [[STButton alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, 40)
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:nil
                                       backgroundImage:nil
                                                 image:nil];
    
    
    STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, 120, view.height)
                                                            text:[NSString stringWithFormat:@"%@",leftTitle]
                                                       textColor:STMC_UICOLOR_FROM_RGBA(0x343434)
                                                            font:13
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentLeft];
    [view addSubview:stoneobs_ui_label];
    [NSString stoneobs_fun_lableAutoAdjustWitdhWithLabel:stoneobs_ui_label];
    stoneobs_ui_label.tag = 10002;
    
    STLabel * stoneobs_ui_s_label = [[STLabel alloc] initWithFrame:CGRectMake(stoneobs_ui_label.right, 0, STONEOBS_UI_UISCREEN_WIDTH -stoneobs_ui_label.right - 20 , view.height)
                                                              text:value
                                                         textColor:STMC_UICOLOR_FROM_RGBA(0x343434)
                                                              font:14
                                                       isSizetoFit:NO
                                                     textAlignment:NSTextAlignmentRight];
    [view addSubview:stoneobs_ui_s_label];
    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_s_label];
    if (stoneobs_ui_s_label.height < view.height) {
        stoneobs_ui_s_label.height = view.height;
    }else{
        view.height = stoneobs_ui_s_label.height + 20;
    }
    stoneobs_ui_label.centerY = view.height/2;
    stoneobs_ui_s_label.tag = 10001;
    UIView * stoneobs_ui_line = [[UIView alloc] initWithFrame:CGRectMake(15, view.height - 0.5, STONEOBS_UI_UISCREEN_WIDTH, 5)];
    stoneobs_ui_line.backgroundColor = self.tableView.backgroundColor;
        [view addSubview:stoneobs_ui_line];
    __weak typeof(self) weakSelf =  self;
    return view;
    
}

- (void)sendDetailRequest{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (self.model.o_id.length ) {
        [stoneobs_net_param_dic setObject:self.model.o_id forKey:@"id"];
    }
    
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/nursery/orderDetail"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * dataDic = responseObject[@"data"];
        self.detailModel = [THBabyDetailOrder mj_objectWithKeyValues:dataDic];
        [self stoneobs_ui_configSubView];
        [self config_table_footer];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {

        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
    
}
@end
